package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.OtherSelectSpecialInventoryAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherSelectSpecialInventoryBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageOtherSelectSpecialInventoryRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;


//验收入库
public class InStorageOtherSelectSpecialInventoryActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView specialInventoryList;

    private List<InStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean> allData;
    private OtherSelectSpecialInventoryAdapter adapter;

    private String mSpecialInventory;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_select_supplier);
        initView(InStorageOtherSelectSpecialInventoryActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageOtherSelectSpecialInventoryActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        specialInventoryList = (PullableListView) activity.findViewById(R.id.supplierList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.supplierListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("查询特殊库存代码");
        ActivityUtils.initBackAndFunction(activity, "其他入库", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        specialInventoryList.setPullUpFlag(false);
        specialInventoryList.setPullDownFlag(false);
        mSpecialInventory = "";
        addSpecialInventoryListData(activity, new ArrayList<>(), specialInventoryList, true);
//        setmScanListener((String scanResult, int length) -> {
//            getData(activity, QRCodeUtils.DecryptQRCode(scanResult));
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void addSpecialInventoryListData(Activity activity, List<InStorageOtherSelectSpecialInventoryBean.DataBean.SpecialInventoryListBean> specialInventoryList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new OtherSelectSpecialInventoryAdapter(activity, specialInventoryList, mSpecialInventory, new InStorageOtherSelectSpecialInventoryListener() {
                @Override
                public NoDoubleClickListener getNoDoubleClickListener(EditText specialInventoryCode) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (TextUtils.isEmpty(specialInventoryCode.getText().toString())) {
                                Utils.middleToast(activity, "特殊库存代码不可以为空");
                            } else {
                                Utils.closeSoftKeyboard(activity, v);
                                mSpecialInventory = specialInventoryCode.getText().toString();
                                getData(activity, mSpecialInventory);
                            }
                        }
                    };
                    return noDoubleClickListener;
                }

                @Override
                public NoDoubleClickListener select(String supplierCode, String supplierDescription) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra(InStorageExtra.CODE, supplierCode);
                            intent.putExtra(InStorageExtra.DESCRIPTION, supplierDescription);
                            activity.setResult(InStorageOtherIndexActivity.SELECT_SPECIAL_INVENTORY, intent);
                            activity.finish();
                        }
                    };
                    return noDoubleClickListener;
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Activity activity, String condition) {
        InStorageOtherSelectSpecialInventoryRequest.request(activity, condition, new GsonBeanListener<InStorageOtherSelectSpecialInventoryBean>() {
            @Override
            public void onSuccess(InStorageOtherSelectSpecialInventoryBean bean) {
                allData = bean.getData().getSpecialInventoryList();
                if (allData == null || allData.isEmpty()) {
                    Utils.middleToast(activity, "未查到数据");
                }
                mSpecialInventory = condition;
                addSpecialInventoryListData(activity, allData, specialInventoryList, true);

            }
        });
    }

    public interface InStorageOtherSelectSpecialInventoryListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText specialInventoryCode);

        NoDoubleClickListener select(String specialInventoryCode, String specialInventoryDescription);
    }

}
